<?php
namespace Home\Controller;
class IndexController extends ParController {
    public function index(){
        $goodsModel = D('Admin/Goods');
    	// 获取疯狂抢购的商品
    	$goods1 = $goodsModel->getPromoteGoods();
    	$goods2 = $goodsModel->getHot();
    	$goods3 = $goodsModel->getBest();
    	$goods4 = $goodsModel->getNew();
    	
    	$this->assign(array(
	    	'goods1' => $goods1,
	    	'goods2' => $goods2,
	    	'goods3' => $goods3,
	    	'goods4' => $goods4,
    	));
        $this->setPageInfo('首页', '关键字', '描述', 1, array('index'), array('index'));
        $this->display();
    }
    
    public function goods(){
        $goodsId = I('get.id');
        $goodsModel = M('Goods');
        $info = $goodsModel->find($goodsId); //商品基本信息
        $gpModel =M('GoodsPics');
        $gpData = $gpModel->where(array('goods_id'=>array('eq',$goodsId)))->select();//商品相册信息
        
        $gaModel = M('GoodsAttr');
        //取出二维数组 商品的可选属性
        $_gaData1 = $gaModel->field('a.*,b.attr_name')->alias('a')
                ->join('left join sp_attribute b on a.attr_id=b.id')->where(array(
                    'a.goods_id' => array('eq',$goodsId),
                    'b.attr_type'=>array('eq',1)
                ))->select();
        $gaData1=array();//处理好的数组
        foreach ($_gaData1 as $k => $v) {
            $gaData1[$v['attr_name']][] = $v;
        }
        //取出二维数组 商品的唯一属性
        $gaData2 = $gaModel->field('a.*,b.attr_name')->alias('a')
                ->join('left join sp_attribute b on a.attr_id=b.id')->where(array(
                    'a.goods_id' => array('eq',$goodsId),
                    'b.attr_type'=>array('eq',0)
                ))->select();
        $this->assign(array(
            'info'=>$info,
            'gpData'=>$gpData,
            'gaData1'=>$gaData1,
            'gaData2'=>$gaData2,
            
        ));
        $this->setPageInfo($info['goods_name'].'-商品详情页', $info['seo_keyword'],
                $info['seo_description'], 0, array('goods','common','jqzoom'), array('goods','jqzoom-core'));
        $this->display();
    }
    
    public function ajaxGetPrice(){
    $goodsId = I('get.goods_id');
    $goodsModel = D('Admin/Goods');
    //在COOKIE中保存一个一维数组，存最近浏览过了10件商品的ID  序列化
    $recentDisplay = isset($_COOKIE['recentDisplay'])?unserialize($_COOKIE['recentDisplay']):array();
    //把刚刚浏览的商品id放在数组最前面
    array_unshift($recentDisplay, $goodsId);
    //去重复
    $recentDisplay=  array_unique($recentDisplay);
    //如果超过10个就只保留前10个
    if(count($recentDisplay)>10){
        $recentDisplay = array_slice($recentDisplay, 0,10);
    }
    $aMonth = 30*24*3600;
    $data = serialize($recentDisplay);
    setcookie('recentDisplay',$data,  time()+$aMonth,'/');

    echo $goodsModel->getMemberPrice($goodsId);
    }
    
    //取出最近浏览过的商品信息
    public function ajaxGetRecentDisGoods(){
        $recentDisplay = isset($_COOKIE['recentDisplay'])?unserialize($_COOKIE['recentDisplay']):array();
        if(!empty($recentDisplay)){
            $goodsModel = M('Goods');
            $recentDisplay_str = implode(',', $recentDisplay);
            $goods = $goodsModel->field('id,goods_name,sm_logo')->where(array('id'=>array('in',$recentDisplay_str)))
                    ->order("instr(',$recentDisplay_str,',concat(',',id,','))")->select();
            echo json_encode($goods);
        }
    }
    
    //取出购物车商品ajax
    public function ajaxGetCart() {
        $cartModel = D('Admin/Cart');
		$data = $cartModel->cartList();
        echo json_encode($data);
    }
    public function ajaxGetComment(){
        $ret = array('login'=>0);
        $mid = session('mid');
        if($mid){
            $ret['login']=1;
        }
        echo json_encode($ret);
    }
}